package com.yyjj.my.fragment;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.aries.ui.view.title.TitleBarView;
import com.aries.ui.widget.alert.UIAlertDialog;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.event.IEvent;
import com.vise.xsnow.event.Subscribe;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.yyjj.my.R;
import com.yyjj.my.activity.SetUpActivity;
import com.yyjj.my.adapter.PersonalAdapter;
import com.yyjj.my.api.UrlContant;
import com.yyjj.my.entity.UserInfoEntity;

import tech.com.commoncore.base.BaseActivity;
import tech.com.commoncore.base.BaseTitleFragment;
import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.manager.GlideManager;
import tech.com.commoncore.user.CurrentUser;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.OnNoDoubleClickListener;


public class PersonalFragment extends BaseTitleFragment {
    UserInfoEntity data;
    LinearLayout ll_my_quanzi;
    LinearLayout fensi_layout;
    LinearLayout ll_zan;
    LinearLayout guanzhu_layout;
    TextView tv_fabu;
    TextView tv_fensi;
    TextView tv_zan;
    TextView tv_guanzhu;
    LinearLayout llFeedback;
    LinearLayout llNews;
    LinearLayout llClean;
    LinearLayout llSettings;
    LinearLayout llLogin;
    RelativeLayout rl_my;

    private LinearLayout mLlHead;
    private ImageView mIvIcon;
    private TextView mTvNick;
    private TextView mTvDec;
    private TextView tv_lr;
    //    private RecyclerView mRecyclerView;
    PersonalAdapter personalAdapter;
    private static CurrentUser currentUser;
    private static final String USER_INFO_KEY = "userInfoKey";
    int likeCount = 0;

    public static PersonalFragment newInstance() {
        Bundle args = new Bundle();
        PersonalFragment fragment = new PersonalFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.my_fragmnet_personal;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mLlHead = (LinearLayout) mContentView.findViewById(R.id.ll_head);
        rl_my = (RelativeLayout) mContentView.findViewById(R.id.rl_my);
        mIvIcon = (ImageView) mContentView.findViewById(R.id.iv_icon);
        mTvNick = (TextView) mContentView.findViewById(R.id.tv_nick);
        mTvDec = (TextView) mContentView.findViewById(R.id.tv_dec);
        tv_lr = (TextView) mContentView.findViewById(R.id.tv_lr);
//        mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.recyclerView);


        ll_my_quanzi = (LinearLayout) mContentView.findViewById(R.id.ll_my_quanzi);
        fensi_layout = (LinearLayout) mContentView.findViewById(R.id.fensi_layout);
        ll_zan = (LinearLayout) mContentView.findViewById(R.id.ll_zan);
        guanzhu_layout = (LinearLayout) mContentView.findViewById(R.id.guanzhu_layout);

        tv_fabu = (TextView) mContentView.findViewById(R.id.tv_fabu);
        tv_fensi = (TextView) mContentView.findViewById(R.id.tv_fensi);
        tv_zan = (TextView) mContentView.findViewById(R.id.tv_zan);
        tv_guanzhu = (TextView) mContentView.findViewById(R.id.tv_guanzhu);


        llFeedback = (LinearLayout) mContentView.findViewById(R.id.ll_feedback);
        llNews = (LinearLayout) mContentView.findViewById(R.id.ll_news);
        llClean = (LinearLayout) mContentView.findViewById(R.id.ll_clean);
        llSettings = (LinearLayout) mContentView.findViewById(R.id.ll_settings);
        llLogin = (LinearLayout) mContentView.findViewById(R.id.ll_login);

        initPageShow();

        onClick();

        initUser();

        tv_lr.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {

                if (ServiceFactory.getInstance().getLoginService().getCurrentUser()!= null) {
                    exitLogin();
//                    Log.w("linbin---","Token="+ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken());
//                    Log.w("linbin---","name=="+ServiceFactory.getInstance().getLoginService().getCurrentUser().getNickName()
//                            +"  Signature=="+ServiceFactory.getInstance().getLoginService().getCurrentUser().getSignature()+
//                            " img=="+ServiceFactory.getInstance().getLoginService().getCurrentUser().getAvatar() );
                }else {
                    ARouter.getInstance().build("/login/login").navigation();
                }

            }
        });

        if (ServiceFactory.getInstance().getLoginService().getCurrentUser()  != null) {
            tv_lr.setText(getResources().getString(R.string.exit_login));
        }else {
            tv_lr.setText(getResources().getString(R.string.my_login_regist));
        }


    }

private void initUser() {
    CurrentUser currentUser = ServiceFactory.getInstance().getLoginService().getCurrentUser();
    if (currentUser != null && currentUser.isLogin) {

    }
    if (ServiceFactory.getInstance().getLoginService().getCurrentUser() == null){
        mIvIcon.setImageResource(R.mipmap.head_portrait1);
        tv_lr.setText(getResources().getString(R.string.my_login_regist));
//        tv_lr.setText(R.string.toLogin);
    }else {
//        loginTv.setText("登录/注册");
        ViseHttp.GET(UrlContant.USERINFO)
                .baseUrl(tech.com.commoncore.constant.ApiConstant.BASE_URL)
                .addHeader("XX-Token", ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken())
                .addHeader("XX-Device-Type", "android")
                .request(new ACallback<UserInfoEntity>() {

                    @Override
                    public void onSuccess(UserInfoEntity data) {
                        if (data.getCode() == 1) {
                            if (!data.getData().getUser_nickname().equals("")) {
                                mTvNick.setText(data.getData().getUser_nickname());
                                ServiceFactory.getInstance().getLoginService().getCurrentUser().setNickName(data.getData().getUser_nickname());
                            } else {
                                mTvNick.setText(data.getData().getMobile());
                                ServiceFactory.getInstance().getLoginService().getCurrentUser().setNickName(data.getData().getMobile());
                            }
                            GlideManager.loadCircleImg(data.getData().getAvatar(), mIvIcon, R.mipmap.head_portrait1);
//                            GlideManager.loadCircleImg(R.mipmap.head_portrait1, mIvIcon);
//                            CacheData.setHeadImg(getContext(),data.getData().getAvatar());
                            ServiceFactory.getInstance().getLoginService().getCurrentUser().setAvatar(data.getData().getAvatar());


                            if (!data.getData().getSignature().equals("")) {
                                mTvDec.setText(data.getData().getSignature());
//                                CacheData.setSign(getContext(),data.getData().getSignature());
                                ServiceFactory.getInstance().getLoginService().getCurrentUser().setSignature(data.getData().getSignature());
                            } else {
                                mTvDec.setText(getResources().getString(R.string.signature_hint));
//                                CacheData.setSign(getContext(),"");
                                ServiceFactory.getInstance().getLoginService().getCurrentUser().setSignature("");
                            }
                        }
//                        Log.w("linbin---","name=="+data.getData().getUser_nickname()+"  Signature=="+data.getData().getSignature()+ " img=="+data.getData().getAvatar() );
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }
}

    private void onClick() {
        ll_my_quanzi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ServiceFactory.getInstance().getLoginService().getCurrentUser()!= null) {
                    ARouter.getInstance().build("/home/MyCommunity").navigation();
                } else {
                    ARouter.getInstance().build("/login/login").navigation();
//                    ARouter.getInstance().build("/home/MyCommunity").navigation();
                }
            }
        });
        fensi_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ServiceFactory.getInstance().getLoginService().getCurrentUser() != null) {
                    ARouter.getInstance().build("/circle/fans").navigation();
                } else {
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
        guanzhu_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ServiceFactory.getInstance().getLoginService().getCurrentUser() != null) {
                    ARouter.getInstance().build("/circle/follow").navigation();
                } else {
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });


        llFeedback.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ARouter.getInstance().build("/my/feedback").navigation();
            }
        });
        llNews.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (ServiceFactory.getInstance().getLoginService().getCurrentUser() != null) {
                    ARouter.getInstance().build("/my/msg").navigation();
                } else {
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
        llClean.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                ((BaseActivity) mContext).showLoading(getResources().getString(R.string.my_cleaning));
                rl_my.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ((BaseActivity) mContext).hideLoading();
                    }
                },1500);
            }
        });
        llSettings.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (ServiceFactory.getInstance().getLoginService().getCurrentUser() != null) {
                    FastUtil.startActivity(mContext,SetUpActivity.class);
                } else {
                    ARouter.getInstance().build("/login/login").navigation();
                }
            }
        });
    }

    private void exitLogin() {
        UIAlertDialog alertDialog = new UIAlertDialog.DividerIOSBuilder(getContext())
                .setTitle(R.string.exit_hint)
                .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setNegativeButton(R.string.exit_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        ServiceFactory.getInstance().getLoginService().logout();

                        BusManager.getBus().post(new LoginChangeEvent(false));
                    }
                })
                .create();
        alertDialog.show();

    }


    private void initPageShow() {
        if (ServiceFactory.getInstance().getLoginService().getCurrentUser()==null) {  //未登录
            GlideManager.loadCircleImg(R.mipmap.head_portrait1, mIvIcon);
            mTvNick.setText(getResources().getString(R.string.my_login_regist));
            tv_lr.setText(getResources().getString(R.string.my_login_regist));
            mTvDec.setText("");
            mLlHead.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (ServiceFactory.getInstance().getLoginService().getCurrentUser() == null) {
                        ARouter.getInstance().build("/login/login").navigation();
                    } else {
                        FastUtil.startActivity(mContext, SetUpActivity.class);
                    }
//                    ARouter.getInstance().build("/login/login").navigation();
                }
            });

        } else {
            tv_lr.setText(getResources().getString(R.string.my_out));


            mTvDec.setText(getResources().getString(R.string.my_sin));
            mLlHead.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    if (ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken() == null) {
                        ARouter.getInstance().build("/login/login").navigation();
                    } else {
                        FastUtil.startActivity(mContext, SetUpActivity.class);
                    }
                }
            });
        }
    }

    @Override
    public boolean isRegisterEvent() {
        return true;
    }

    @Subscribe
    public void update(IEvent event) {
        initPageShow();
        initUser();
    }


    @Subscribe
    public void update(LoginChangeEvent event) {
        initPageShow();
        initUser();
//        if (ServiceFactory.getInstance().getLoginService().getCurrentUser().getToken() == null) {
//            tv_fabu.setText("0");
//            tv_fensi.setText("0");
//            tv_zan.setText("0");
//            tv_guanzhu.setText("0");
//        } else {
////            myCommunity();
//        }
    }

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("").setTitleMainTextColor(Color.WHITE).setBgColor(Color.TRANSPARENT);
    }
}
